<?php 
	class View extends AT {
		private $datas = array();
		private $elementDatas = array();
		private $comp = array();
		private $layout = array();
		private $css = array();
		private $js = array();
		public function __construct($layout=array("layouts"=>"default"),$comp=array(),$datas=array()){
			parent::__construct();
			$this->layout=(isset($layout)&&!empty($layout))?$layout:array("layouts"=>"default");
			$this->comp=$comp;
			$this->datas=$datas;
		}
		public function getDatas(){
			return $this->datas;
		}
		public function render(){
			extract($this->datas,EXTR_SKIP);
			ob_start();
			include($this->getLayout());
			ob_get_flush();
		}
		private function getLayout(){
			foreach($this->layout AS $folder => $layout )
				return Basic::fileExist(APP_VIEW.strtolower($folder).DS.$layout.".at")?(APP_VIEW.strtolower($folder).DS.($layout).".at"):die("Class : View<br/>Function : getLayout()<br/>Error layout '".$this->layout["layouts"]."' not exists.");
		}
		public function layout($controller=array(),$var=array()){
			$controller=(isset($controller)&&is_array($controller)&&!empty($controller))?$controller:array();
			$layout = "" ;
			if(!empty($controller)){
				foreach($controller AS $package => $template ){
					if(Basic::fileExist(APP_VIEW.strtolower(Basic::getPlural($package)).DS.($template).".at")){
						$layout = (APP_VIEW.strtolower(Basic::getPlural($package)).DS.($template).".at") ;
						break;
					}else{
						die("Class : View<br/>Function : layout()<br/>Error default layout '".strtolower(Basic::getPlural($package))." => $template' not exists.");
					}
				}
				extract($this->datas,EXTR_SKIP);
				extract($var,EXTR_OVERWRITE);
				ob_start();
				include($layout);
				ob_get_flush();
			}
		}
		public function getElementVar($name){
			return isset($this->elementDatas[$name])?$this->elementDatas[$name]:null;
		}
		public function element($name="",$var=array()){
			$element=(isset($name)&&$name!==""&&$name!==NULL)?$name:NULL;
			if($element!=NULL){
				if(Basic::fileExist(APP_VIEW."elements".DS.($element).".at")){
					$element = (APP_VIEW."elements".DS.($element).".at") ;
				}else{
					die("Class : View<br/>Function : element()<br/>Error element '$element' not exists.");
				}
				extract($this->datas,EXTR_SKIP);
				$this->elementDatas = array_merge($this->elementDatas,$var);
				ob_start();
				include($element);
				ob_get_flush();
			}
		}
		public function getPath($path=array("page"=>"index"),$vars=array()){
			if(!empty($path)){
				if(isset($_SERVER["QUERY_STRING"])&&$_SERVER["QUERY_STRING"]!==""){
					$link = array();
					foreach($path as $controller =>$action ){
						$link[]="com=".$controller."&action=".$action;
						foreach($vars as $key =>$value )$link[]=$key."=".$value;
						$link = "index.php?".implode("&",$link) ;
						break;
					}
				}
				else{
					foreach($path as $controller =>$action ){
						$link = "index.php/".$controller."/".$action;
						foreach($vars as $key=>$val)$link.="/".$key.":".$val;
						break;
					}
				}
				return ROOT . $link;
			}
		}
	}
